//
//  UIImageView+Request.m
//  DatangLife
//
//  Created by 陈建 on 2018/6/7.
//  Copyright © 2018年 DatangLife. All rights reserved.
//

#import "UIImageView+Request.h"
#import <YYWebImage.h>
@implementation UIImageView (Request)
- (void)setImageWithUrlString:(NSString *)urlStr{
    [self setImageWithUrlString:urlStr palceHolderImage:nil];
}
- (void)setImageWithURL:(NSURL *)URL{
    [self setImageWithURL:URL palceHolderImage:nil];
}

- (void)setImageWithUrlString:(NSString *)urlStr palceHolderImage:(UIImage *)image{
    [self setImageWithUrlString:urlStr palceHolderImage:image finishHandle:nil];
}
- (void)setImageWithURL:(NSURL *)URL palceHolderImage:(UIImage *)image{
    [self setImageWithURL:URL palceHolderImage:image finishHandle:nil];
}

- (void)setImageWithUrlString:(NSString *)urlStr palceHolderImage:(UIImage *)image finishHandle:(void (^)(BOOL, UIImage *))finishHandle{
    [self setImageWithUrlString:urlStr palceHolderImage:image progressHandle:nil finishHandle:finishHandle];
}
- (void)setImageWithURL:(NSURL *)URL palceHolderImage:(UIImage *)image finishHandle:(void (^)(BOOL, UIImage *))finishHandle{
    [self setImageWithURL:URL palceHolderImage:image progressHandle:nil finishHandle:finishHandle];
}

- (void)setImageWithUrlString:(NSString *)urlStr palceHolderImage:(UIImage *)image progressHandle:(void (^)(CGFloat))progressHandle finishHandle:(void (^)(BOOL, UIImage *))finishHandle{
    [self setImageWithURL:[NSURL URLWithString:urlStr] palceHolderImage:image progressHandle:progressHandle finishHandle:finishHandle];
}
- (void)setImageWithURL:(NSURL *)URL palceHolderImage:(UIImage *)image progressHandle:(void (^)(CGFloat))progressHandle finishHandle:(void (^)(BOOL, UIImage *))finishHandle{
    
    [self yy_setImageWithURL:URL placeholder:image options:YYWebImageOptionShowNetworkActivity progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        if (progressHandle) {
            progressHandle(receivedSize * 1.0 /expectedSize);
        }
    } transform:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        NSLog(@"%@",error);
        if (finishHandle) {
            finishHandle(error == nil,image);
        }
    }];
}
@end
